﻿Public Class FloorManagemant
    Dim DAFloor As New DSFloorManagemantTableAdapters.FLOORSTableAdapter
    Dim DACategory As New DSFloorManagemantTableAdapters.ROOM_CATEGORYTableAdapter
    Dim DARoom As New DSFloorManagemantTableAdapters.ROOMSTableAdapter
    Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()
        With CboCategoryRoom
            .DataSource = DACategory.GetData
            .ValueMember = "CATEGORY_ID"
            .DisplayMember = "GATEGORY_NAME"
            .SelectedIndex = -1
        End With

        With CboFloor
            .DataSource = DAFloor.GetData
            .ValueMember = "FLOOR_ID"
            .DisplayMember = "FLOOR_NAME"
            .SelectedIndex = -1
        End With
        ' Add any initialization after the InitializeComponent() call.

    End Sub
    Private Sub ToolStripButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton3.Click
        Dim Floor As New FormFloor
        If Floor.ShowDialog = DialogResult.OK Then
            RefreshNodeFloor()
        End If
    End Sub
    Private Sub PopulateNodesFloor(ByVal dtbl As DataTable, ByVal nodes As TreeNodeCollection)
        nodes.Clear()
        For Each dr As DataRow In dtbl.Rows
            Dim tn As New TreeNode
            tn.Name = dr("FLOOR_ID").ToString
            tn.Text = dr("FLOOR_NAME").ToString
            tn.Tag = dr("FLOOR_DESCRIP").ToString
            nodes.Add(tn)
            tn.ImageIndex = 0
        Next
    End Sub
    Private Sub PopulateNodesCategoryRoom(ByVal dtbl As DataTable, ByVal nodes As TreeNodeCollection)
        nodes.Clear()
        For Each dr As DataRow In dtbl.Rows
            Dim tn As New TreeNode
            tn.Name = dr("CATEGORY_ID").ToString
            tn.Text = dr("GATEGORY_NAME").ToString
            tn.Tag = dr("GATEGORY_DESCRIPTION").ToString
            nodes.Add(tn)
            tn.ImageIndex = 0
        Next
    End Sub
    Private Sub FloorManagemant_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        RefreshNodeFloor()
        RefreshNodeCategoryRoom()
        

    End Sub
    Private Sub RefreshNodeFloor()
        Dim tbl As DataTable = DAFloor.GetData
        PopulateNodesFloor(tbl, TreeFloor.Nodes)

    End Sub
    Private Sub RefreshNodeCategoryRoom()
        Dim tblCategor As DataTable = DACategory.GetData
        PopulateNodesCategoryRoom(tblCategor, TreeCategoryRoom.Nodes)
        TreeFloor.Focus()
        TreeFloor.Select()
    End Sub



    Private Sub EditFloorToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EditFloorToolStripMenuItem.Click
        Try
            Dim TNode As TreeNode = TreeFloor.SelectedNode
            Dim Floor As New FormFloor
            Floor.LblSaveOption.Text = TNode.Name
            Floor.TxtFloor.Text = TNode.Text
            Floor.TxtFloorDescrip.Text = TNode.Tag
            If Floor.ShowDialog = DialogResult.OK Then
                RefreshNodeFloor()
            End If
        Catch ex As Exception

        End Try
        

    End Sub

    Private Sub ToolStripMenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripMenuItem1.Click
        Dim FCategoryRoom As New FormCategoryRoom
        If FCategoryRoom.ShowDialog = Windows.Forms.DialogResult.OK Then
            RefreshNodeCategoryRoom()
        End If
    End Sub

    Private Sub EditCategoryRoomToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles EditCategoryRoomToolStripMenuItem.Click
        Try
            Dim TNode As TreeNode = TreeCategoryRoom.SelectedNode
            Dim FCategoryRoom As New FormCategoryRoom
            FCategoryRoom.lblSaveOption.Text = TNode.Name
            FCategoryRoom.TxtRoomCategory.Text = TNode.Text
            FCategoryRoom.TxtRoomCategoryDes.Text = TNode.Tag
            If FCategoryRoom.ShowDialog = Windows.Forms.DialogResult.OK Then
                RefreshNodeCategoryRoom()
            End If
        Catch ex As Exception

        End Try
        
    End Sub

    Private Sub DeleteCategoryRoomToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DeleteCategoryRoomToolStripMenuItem.Click
        Dim TNode As TreeNode = TreeCategoryRoom.SelectedNode
        If MessageBox.Show("Do you want delete this category?", "Room", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
            If DACategory.IS_DELETE(True, TNode.Name) = 1 Then
                RefreshNodeCategoryRoom()
            End If
        End If
    End Sub

    Private Sub ToolStripButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton1.Click
        Dim FRoom As New FormNewRoom
        If FRoom.ShowDialog() = DialogResult.OK Then
            RefreshLVRoom()
        End If
    End Sub

    Private Sub ShowDataInLvw(ByVal data As DataTable, ByVal lvw As ListView)
        ' lvw.View = View.Details
        'lvw.GridLines = True
        lvw.Columns.Clear()
        lvw.Items.Clear()
        Dim lst As ListViewItem
        'For Each col As DataColumn In data.Columns
        '    lvw.Columns.Add(col.ToString)
        'Next
        For Each row As DataRow In data.Rows

            lst = lvw.Items.Add(If(row(3) IsNot Nothing, row(3).ToString, ""))
            lst.ImageIndex = 0
            lst.Tag = row
            'For i As Integer = 1 To data.Columns.Count - 1
            '    lst.SubItems.Add(If(row(i) IsNot Nothing, row(i).ToString, ""))
            'Next
        Next
    End Sub
    Private Sub RefreshLVRoom()
        Dim tblRoom As DataTable = DARoom.GetData
        ShowDataInLvw(tblRoom, ListViewRoom)
    End Sub

    Private Sub TreeFloor_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeFloor.AfterSelect
        Dim dNode As TreeNode = TreeFloor.SelectedNode
        Dim tblFloor As DataTable = DARoom.SelectByFloor(dNode.Name)
        ShowDataInLvw(tblFloor, ListViewRoom)
        CleanRoomInfo()
    End Sub
    Private Sub ToolStripButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ToolStripButton2.Click
        Dim dNode As TreeNode = TreeFloor.SelectedNode
        Dim tblFloor As DataTable = DARoom.GetData
        ShowDataInLvw(tblFloor, ListViewRoom)
    End Sub

    'Imports System.Data.SqlClient
    'Public Class Form1

    '    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    '        Dim con As New SqlConnection("Data Source=localhost; Initial Catalog= Product; User ID= sa; Password = 123456")
    '        con.Open()
    '        Dim sql As String = "SELECT * FROM tblProduct"
    '        Dim com As New SqlCommand(sql, con)
    '        Dim da As New SqlDataAdapter
    '        Dim ds As New DataSet
    '        da.SelectCommand = com
    '        da.Fill(ds, "tbl")
    '        ShowDataInLvw(ds.Tables("tbl"), ListView1)
    '    End Sub
    '    Private Sub ShowDataInLvw(ByVal data As DataTable, ByVal lvw As ListView)
    '        lvw.View = View.Details
    '        lvw.GridLines = True
    '        lvw.Columns.Clear()
    '        lvw.Items.Clear()
    '        For Each col As DataColumn In data.Columns
    '            lvw.Columns.Add(col.ToString)
    '        Next

    '        For Each row As DataRow In data.Rows
    '            Dim lst As ListViewItem
    '            lst = lvw.Items.Add(row(0))
    '            For i As Integer = 1 To data.Columns.Count - 1
    '                lst.SubItems.Add(row(i))
    '            Next
    '        Next
    '    End Sub
    'End Class
    Private Sub CleanRoomInfo()
        LblSaveOption.Text = "0"
        TxtRoomName.Text = ""
        CboCategoryRoom.SelectedIndex = -1
        CboFloor.SelectedIndex = -1
        TxtBed.Text = ""
        TxtDescriptRoom.Text = ""
    End Sub

   
    

    Private Sub ListViewRoom_ItemSelectionChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ListViewItemSelectionChangedEventArgs) Handles ListViewRoom.ItemSelectionChanged

        Dim row As DataRow = CType(e.Item.Tag, DataRow)
        LblSaveOption.Text = row("ROOM_ID")
        TxtRoomName.Text = row("ROOM_NAME")
        CboCategoryRoom.SelectedValue = row("CATEGORY_ID")
        CboFloor.SelectedValue = row("FLOOR_ID")
        TxtBed.Text = row("ROOM_BED")
        TxtDescriptRoom.Text = row("ROOM_DESCRPTION")
    End Sub

    Private Sub BtnEditRoomInfo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnEditRoomInfo.Click
        Try
            Dim row As DataRow = CType(ListViewRoom.SelectedItems(0).Tag, DataRow)
            Dim FNRoom As New FormNewRoom
            FNRoom.LblSaveOption.Text = row("ROOM_ID")
            FNRoom.TxtRoomName.Text = row("ROOM_NAME")
            FNRoom.CboFloor.SelectedValue = row("FLOOR_ID")
            FNRoom.CboCategoryRoom.SelectedValue = row("CATEGORY_ID")
            FNRoom.TxtBed.Text = row("ROOM_BED")
            FNRoom.TxtDescriptRoom.Text = row("ROOM_DESCRPTION")
            If FNRoom.ShowDialog = DialogResult.OK Then
                Dim dNode As TreeNode = TreeFloor.SelectedNode
                Dim tblFloor As DataTable = DARoom.GetData
                ShowDataInLvw(tblFloor, ListViewRoom)
            End If
        Catch ex As Exception

        End Try
    End Sub

   
End Class
